package fpt.edu.vn.action;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fpt.edu.vn.dao.TopicsDao;
import fpt.edu.vn.util.SolrQuerying;

/* 
 * DetailAction.java 
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class DeleteTopicAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private int topicId;

	public int getTopicId() {
		return topicId;
	}

	public void setTopicId(int topicId) {
		this.topicId = topicId;
	}

	private List<Object> checkOwnerTopic;
	private List<Object> checkAdmin;
	private int isOwner;
	private int isAdmin;
	private String Username;
	private boolean isdeleted;

	public String getUsername() {
		return Username;
	}

	public void setUsername(String username) {
		Username = username;
	}

	// For SessionAware
	Map<String, Object> session;
	private SolrQuerying solQ = new SolrQuerying();
	private TopicsDao dao = new TopicsDao();

	public String execute() {
		String email = "";
		session = ActionContext.getContext().getSession();
		if (session.containsKey("emailss")) {
			email = (String) session.get("emailss");
			// get email, userid to compare with user login
			checkOwnerTopic = dao.checkOwnerTopic(email, topicId);
			checkAdmin = dao.checkAdmin(email);
			if (checkAdmin.size() < 1) {
			} else {
				setIsAdmin(1);
			}
			if (checkOwnerTopic.size() < 1) {
			} else {
				setIsOwner(1);
			}

		}
		if (topicId == 0 || dao.detail(topicId).size() == 0) {
			return NONE;
		} else {
			if (isAdmin == 1 || isOwner == 1) {

				dao.deletedTopicImg(topicId);
				dao.deletedTopicFav(topicId);
				dao.deletedTopicSpam(topicId);
				dao.deletedTopicRate(topicId);
				dao.deletedActivity(topicId);
				dao.deletedRefresh(topicId);
				if (dao.deletedTopic(topicId)) {
					isdeleted = true;
				}
			}
			if (isdeleted) {
				try {
					solQ.updateDataFromMysqlToSolr();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return SUCCESS;
			} else {
				return ERROR;
			}
		}
	}

	public int getIsOwner() {
		return isOwner;
	}

	public void setIsOwner(int isOwner) {
		this.isOwner = isOwner;
	}

	public int getIsAdmin() {
		return isAdmin;
	}

	public void setIsAdmin(int isAdmin) {
		this.isAdmin = isAdmin;
	}

}
